package com.luke.star.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.luke.star.bean.BaseResBean;
import com.luke.star.bean.Pager;
import com.luke.star.bean.StTaskVo;
import com.luke.star.bean.TaskProcessBean;
import com.luke.star.enums.AssiCircleModelEnum;
import com.luke.star.enums.ErrorCodeEnum;
import com.luke.star.enums.StTaskStateEnum;
import com.luke.star.helper.serviceImpl.StTaskHelperService;
import com.luke.star.persist.mapper.StAssignmentMapper;
import com.luke.star.persist.mapper.StCommonMapper;
import com.luke.star.persist.mapper.StTaskMapper;
import com.luke.star.persist.mapper.StTaskNoteMapper;
import com.luke.star.persist.po.StAssignment;
import com.luke.star.persist.po.StTask;
import com.luke.star.persist.po.StTaskNote;
import com.luke.star.util.ReqCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class StTaskService {
	@Resource
	private StTaskMapper stTaskMapper;
	@Resource
	private StAssignmentMapper stAssignmentMapper;
	@Resource
	private StTaskNoteMapper stTaskNoteMapper;
	@Resource
	private StCommonMapper stCommonMapper;
	@Resource
	private StTaskHelperService stTaskHelperService;
	
	public Pager queryTaskByPage(Pager page,Map<String,Object> queryParam) {
		List<StTaskVo> list = stTaskMapper.listStTaskByPage(queryParam);
		int count = stTaskMapper.countStTaskByPage(queryParam);
		page.setResult(list);
		page.setTotalCount(count);
		return page;
	}
	
	public Pager queryTaskLogByPage(Pager page,Map<String,Object> queryParam) {
		List<StTaskNote> list = stTaskNoteMapper.listTaskLogByPage(queryParam);
		int count = stTaskNoteMapper.countTaskLogByPage(queryParam);
		page.setResult(list);
		page.setTotalCount(count);
		return page;
	}
	
	public BaseResBean addTaskByHuman(StTask reqRecord){
		BaseResBean res = reqCheck(reqRecord);
		if(!ErrorCodeEnum.s0000.code.equals(res.getResCode())) {
			return res;
		}
		reqRecord.setId(stCommonMapper.createId22());
		reqRecord.setCreateTime(new Date());
		reqRecord.setUpdateTime(new Date());
		reqRecord.setStarNum(0);
		reqRecord.setState(StTaskStateEnum.doing.code);
		reqRecord.setUserid("oGHLM5tBQGuFuxOezSZhTBkacU5s");
		reqRecord.setWeight(1);
		int count = stTaskMapper.insertSelective(reqRecord);
		log.info("任务插入条数[{}]",count);
		
		res.setResCode(ErrorCodeEnum.s0000.code);
		res.setResDesc("添加成功");
		return res;
	}
	public BaseResBean updateProcess(TaskProcessBean reqBean){
		BaseResBean res = reqCheck(reqBean);
		if(!ErrorCodeEnum.s0000.code.equals(res.getResCode())) {
			return res;
		}
		
		Integer num = Integer.valueOf(reqBean.getNumStr());
		
		StTask taskinfo = stTaskMapper.selectByPrimaryKey(reqBean.getTaskid());
		if(taskinfo == null) {
			res.setResCode(ErrorCodeEnum.s0002.code);
			res.setResDesc("任务["+reqBean.getTaskid()+"]不存在");
			return res;
		}
		
		int starNum = taskinfo.getStarNum()==null ? 0 : taskinfo.getStarNum();
		StTask record = new StTask();
		record.setId(taskinfo.getId());
		record.setUpdateTime(new Date());
		record.setStarNum(starNum+num);
		
		StTaskNote noteRecord = new StTaskNote();
		noteRecord.setTaskId(taskinfo.getId());
		noteRecord.setCreateTime(new Date());
		noteRecord.setAddStarNum(num);
		noteRecord.setNowStarNum(starNum+num);
		if(!StringUtils.isEmpty(reqBean.getNote())) {
			noteRecord.setNote(reqBean.getNote());
		}
		
		stTaskHelperService.modifyTaskProgress(record, noteRecord);
		
		log.info("任务进度调整成功,taskid[{}],taskName[{}],addNum[{}]",taskinfo.getId(),taskinfo.getTaskName(),num);
		res.setResCode(ErrorCodeEnum.s0000.code);
		res.setResDesc("任务进度调整成功");
		
		return res;
	}
	
	public BaseResBean addAssignmentByHuman(StAssignment reqRecord){
		
		BaseResBean res = reqCheck(reqRecord);
		if(!ErrorCodeEnum.s0000.code.equals(res.getResCode())) {
			return res;
		}
		reqRecord.setAssiId(stCommonMapper.createId22());
		reqRecord.setAssiCreateTime(new Date());
		reqRecord.setAssiUpdateTime(new Date());
		reqRecord.setAssiRunTime(0);
		int count = stAssignmentMapper.insertSelective(reqRecord);
		log.info("任务分解插入条数[{}]",count);
		
		res.setResCode(ErrorCodeEnum.s0000.code);
		res.setResDesc("添加成功");
		return res;
	}
	public BaseResBean updateAssignmentByHuman(StAssignment reqRecord){
		BaseResBean res = reqCheck(reqRecord);
		if(!ErrorCodeEnum.s0000.code.equals(res.getResCode())) {
			return res;
		}
		if(StringUtils.isEmpty(reqRecord.getAssiId())) {
			res.setResCode(ErrorCodeEnum.s0001.code);
			res.setResDesc("任务分解ID不能为空");
			return res;
		}
		StAssignment info = stAssignmentMapper.selectByPrimaryKey(reqRecord.getAssiId());
		if(info == null) {
			res.setResCode(ErrorCodeEnum.s0002.code);
			res.setResDesc("任务分解["+reqRecord.getAssiId()+"不存在，更新失败]");
			return res;
		}
		
		reqRecord.setAssiUpdateTime(new Date());
		int count = stAssignmentMapper.updateByPrimaryKeySelective(reqRecord);
		log.info("任务分解更新条数[{}]，assiId[{}]",count,reqRecord.getAssiId());
		
		res.setResCode(ErrorCodeEnum.s0000.code);
		res.setResDesc("更新成功");
		return res;
	}
	private BaseResBean reqCheck(Object obj){
		BaseResBean res = new BaseResBean();
		res.setResCode(ErrorCodeEnum.s0000.code);
		try {
			String validator = ReqCheckUtil.getValidator(obj);
			if(!StringUtils.isEmpty(validator)) {
				log.error("参数校验不通过[{}]",validator);
				res.setResCode(ErrorCodeEnum.s0001.code);
				res.setResDesc(validator);
			}
		}catch (Exception e) {
			log.error("参数校验不通过，",e);
			res.setResCode(ErrorCodeEnum.s0001.code);
			res.setResDesc(ErrorCodeEnum.s0001.desc);
		}
		return res;
	}
	
}
